# -*- encoding : utf-8 -*-
#-------------------------------------
# Triet
#-------------------------------------
class Admin::BookBorrowController < ApplicationController
  # specific layout
  layout 'admin/application'
  
  def index
    
  end
  
  # show form to borrow book
  def borrow
    
  end
  
  # show form to return book
  def return
    
  end
  
  # check reader
  def check_reader
    reader = User.where("card_id = ?", params[:card_id]).first
    if reader
      session[:reader] = nil
      session[:reader] = reader
      flash[:reader_notice] = nil
      
      # get the books which this user is borrowing
      borrowing_books = BookBorrow.where("user_id = ? AND status = ?", reader.id, 0).all
      session[:borrowing_books] = nil
      session[:borrowing_books] = borrowing_books
    else
      flash[:reader_notice] = 'Not found reader'
    end
    
    if params[:type] == 'borrow'
      render 'borrow'
    else
      render 'return'
    end
  end
 
 # process borrowing book
 def borrow_book
   book = Book.where("id = ? AND free_quantity > ?", params[:book_id], 0).first
   if book
     session[:book] = nil
     session[:book] = book
     flash[:book_notice] = nil
     
     # add book to borrowing books
     session[:borrowing_books]  # declare
     
     reader = session[:reader]
     borrow_rule = BorrowRule.where('role_id = ?', reader.role_id).first
     borrow_time = borrow_rule.borrow_time
     
     last = session[:borrowing_books].length
     if last >= borrow_rule.max_book
       flash[:book_notice] = "Your borrowing books is max"
     else
       new_borrow_book = BookBorrow.new(
        :user_id => reader.id,
        :book_id => book.id,
        :borrowed_time => Time.now,
        :return_time => (Time.now + borrow_time.days),
        :status => 0
       )
       
       added = false
       for borrowing in session[:borrowing_books]
         if borrowing.book_id == new_borrow_book.book_id
           added = true
         end
       end
       
       if !added
         session[:borrowing_books][last] = new_borrow_book
       else
         flash[:book_notice] = "You have borrowed this book!"
       end
   
     end
   else
     session[:book] = nil
     flash[:book_notice] = "Book not found"
   end

   render 'borrow'
 end
 
 def delete
   session[:borrowing_books].delete_at(params[:id].to_i)
   render 'borrow'
 end
 
 # proccess returning a book or extending time
 def return_book
   book = Book.where("id = ?", params[:book_id]).first
   if book
     session[:book] = nil
     session[:book] = book
     flash[:book_notice] = nil
     
     book_is_borrowing = false
     session[:borrowing_books].each do |borrowing|
       if borrowing.book_id == book.id
         if params[:return_book]
           # check punishment
           if borrowing.return_time.to_i < Time.now.to_i
             punished_days = ((Time.now.to_i - borrowing.return_time.to_i) / (24*3600) + 1).to_i
             reader_group = borrowing.user.role
             rule = BorrowRule.where('role_id = ?', reader_group).first
             punished_money = rule.punish_money * punished_days
             
             if session[:punished_books]
              last = session[:punished_books].length
             else
               session[:punished_books] = []
               last = 0
             end
             punished_book = Punishment.new(
              :user_id => borrowing.user.id,
              :book_id => borrowing.book_id,
              :money => punished_money
             )
             punished_book.save
             session[:punished_books][last] = punished_book
           end
           # end check punishment
           
           session[:borrowing_books].delete_at(session[:borrowing_books].index(borrowing))
           book_borrow = BookBorrow.find(borrowing.id)
           new_free_quantity = book.free_quantity + 1
           book_borrow.update_attributes(:status => 1)
           book.update_attributes(:free_quantity => new_free_quantity)
         elsif params[:extend_time]
           borrow_rule = BorrowRule.where('role_id = ?', session['reader'].role_id).first
           return_time = borrowing.return_time + borrow_rule.extended_time.days
           borrowing.return_time = return_time
           book_borrow = BookBorrow.find(borrowing.id)
           book_borrow.update_attributes(:return_time => return_time)
         end         
         book_is_borrowing = true
       end
     end
     
     if book_is_borrowing == false
       flash[:book_notice] = "You have not borrowed this book!"
     else
       if params[:return_book]
        flash[:book_notice] = 'This book is returned successfully!'
       elsif params[:extend_time]
        flash[:book_notice] = 'This book is extended time successfully!'
       end
     end
   else
     session[:book] = nil
     flash[:book_notice] = "Book not found"
   end

   render 'return'
 end
 
 def agree_or_clear
   if params[:agree_borrow]
     # save to database
     for borrow_book in session[:borrowing_books] do
       borrowing = BookBorrow.where('id = ? AND status = ?', borrow_book.id, 0).first
       if borrowing == nil
         borrow_book.save
       end
     end
     flash[:borrow_notice] = 'Borrow successfully!'
   elsif params[:clear]
    session[:reader] = nil
    session[:book] = nil
    session[:borrowing_books] = nil
    session[:punished_books] = nil
    
    flash[:book_notice] = nil
    flash[:reader_notice] = nil
    flash[:borrow_notice] = nil
   end
   
   if params[:type] == 'borrow'
      render 'borrow'
   else
      render 'return'
   end
 end
end
